package com.linewell.service.impl;

import com.linewell.dao.PointMapper;
import com.linewell.entity.CommentPoint;
import com.linewell.entity.Point;
import com.linewell.service.IPointService;
import com.linewell.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Title: zmt
 * @description:
 * @author: hyuhuang@linewell.com
 * @since:2018/2/12
 */
@Service
public class PointService implements IPointService{
    @Autowired
    PointMapper pointDao;

    public Msg checkIsFavor(Long userId, Long pointId) {
        List<Point> pointList = pointDao.isExistPoint(userId,pointId);
       if (pointList.size() == 0) {
            pointDao.insertPoint(userId,pointId);
            return Msg.fail1();
       } else {
           if (pointList.get(0).getIsFavorType() == 1) {
               return Msg.success();
           } else if (pointList.get(0).getIsFavorType() == 0) {
               return Msg.fail();
           } else if (pointList.get(0).getIsFavorType() == 2) {
               return Msg.fail1();
           }
       }
       return Msg.fail1();
    }

    public Msg updatePoint(Long userId, Long pointId, int isFavorType, int zanNum, int caiNum) {

        switch (isFavorType){
            //取消赞
            case 0 : zanNum--;isFavorType = 2;break;
            //点赞，取消踩
            case 1 : zanNum++;caiNum--; isFavorType = 1;break;
            //点赞
            case 2 : zanNum++;isFavorType = 1;break;
            //取消踩
            case 3 : caiNum--;isFavorType = 2;break;
            //点踩，取消赞
            case 4 : caiNum++;zanNum--;isFavorType = 0;break;
            //点踩
            default: caiNum++;isFavorType = 0;break;
        }
        pointDao.updateZanOrCai(pointId,zanNum,caiNum);
        pointDao.updatePoint(userId,pointId,isFavorType);

        return Msg.success().add("Type",isFavorType).add("zanNum",zanNum).add("caiNum",caiNum);
    }

    public Msg updateCollect(Long userId, Long pointId, int isCollectType, int collectNum) {
        //取消收藏
        if (isCollectType == 0){
            pointDao.insertCollect(pointId,userId);
            collectNum++;
            isCollectType++;
        } else {
            pointDao.deleteCollect(pointId,userId);
            collectNum--;
            isCollectType--;
        }

        pointDao.updatePointCollect(userId,pointId,isCollectType);
        pointDao.updateCollectNum(collectNum,pointId);
        return Msg.success().add("Type",isCollectType).add("collectNum",collectNum);
    }

    public Msg checkIsCollect(Long userId, Long pointId) {
        Point point = pointDao.checkCollectType(userId,pointId);
        if (point.getIsColletctType() == 1) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    public Msg checkIsCommentFavor(Long replyId, Long userId) {
        List<CommentPoint> pointList = pointDao.checkIsCommentFavor(replyId,userId);
        if (pointList.size() == 0) {
            pointDao.insertCommentPoint(replyId,userId);
            return Msg.fail1();
        } else {
            if (pointList.get(0).getIsCommentType() == 1) {
                return Msg.success();
            } else if (pointList.get(0).getIsCommentType() == 0) {
                return Msg.fail();
            } else if (pointList.get(0).getIsCommentType() == 2) {
                return Msg.fail1();
            }
        }
        return Msg.fail1();
    }


    public Msg updateCommentPoint(Long replyId, Long userId, int isFavorType, int zanNum, int caiNum) {
        switch (isFavorType){
            //取消赞
            case 0 : zanNum--;isFavorType = 2;break;
            //点赞，取消踩
            case 1 : zanNum++;caiNum--; isFavorType = 1;break;
            //点赞
            case 2 : zanNum++;isFavorType = 1;break;
            //取消踩
            case 3 : caiNum--;isFavorType = 2;break;
            //点踩，取消赞
            case 4 : caiNum++;zanNum--;isFavorType = 0;break;
            //点踩
            default: caiNum++;isFavorType = 0;break;
        }
        pointDao.updateReplyZanOrCai(replyId,zanNum,caiNum);
        pointDao.updateCommentPoint(replyId,userId,isFavorType);

        return Msg.success().add("Type",isFavorType).add("zanNum",zanNum).add("caiNum",caiNum);
    }
}
